package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.entity.ChargingFlow;
import com.antdesignbackend.backend.entity.Log;
import com.antdesignbackend.backend.entity.Registorder;
import com.antdesignbackend.backend.entity.Room;
import com.antdesignbackend.backend.service.ChargingFlowService;
import com.antdesignbackend.backend.service.ILogService;
import com.antdesignbackend.backend.service.IRoomService;
import com.antdesignbackend.backend.service.RegistorderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/home")
public class HomeController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    ReturnVO result = new ReturnVO();
    private Integer userId;
    private String roleID;
    @Autowired
    private ChargingFlowService chargingFlowService;
    @Autowired
    private ILogService logService;
    @Autowired
    private RegistorderService registorderService;
    @Autowired
    private IRoomService roomService;


    /**
     * 首页 index
     */
    @GetMapping("/index")
    public String index(HttpServletRequest request) {
        // ModelAndView modelAndView = new ModelAndView("client/index");
        //获取cookies信息 roleID 1超级管理员，2管理员，3商户
        userId = (Integer) request.getSession().getAttribute("UserID");
        roleID = (String) request.getSession().getAttribute("RoleID");
        return "index";
    }

    @GetMapping("/getIncome")
    @ResponseBody
    public Double getIncome() throws SecurityException, IllegalArgumentException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String newTime = sdf.format(new Date());
        List<ChargingFlow> chargingFlows = chargingFlowService.list();
        Double sum = 0.0;
        for (ChargingFlow chargingFlow : chargingFlows) {
            if (chargingFlow.getUserId() == userId) {
                String oldTime = chargingFlow.getCreateTime();
                String[] strs = oldTime.split(" ");
                String oldTime2 = strs[0];
                if (newTime.equals(oldTime2)) {
                    sum = sum + chargingFlow.getMoney();
                }
            }
        }
        return sum;
    }
    @GetMapping("/getIncome2/{type}")
    @ResponseBody
    public Map<String,List<?>> getIncome2(@PathVariable String type) throws Exception{
            Map<String,List<?>> map=new HashMap<>();
            Double sum;
            List<Double> doubles=new ArrayList<>();
            List<String> strings=new ArrayList<>();
            if(type.equals("today")){
                sum=0.0;
                System.out.println("进入今日金额！！！！！！！");
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String newTime = sdf.format(new Date());
                List<ChargingFlow> chargingFlows = chargingFlowService.list();
                for (ChargingFlow chargingFlow : chargingFlows) {
                    if (chargingFlow.getUserId() == userId) {
                        String oldTime = chargingFlow.getCreateTime();
                        String[] strs = oldTime.split(" ");
                        String oldTime2 = strs[0];
                        if (newTime.equals(oldTime2)) {
                            sum = sum + chargingFlow.getMoney();
                        }
                    }
                }
                doubles.add(sum);
                strings.add(newTime);
                map.put("money",doubles);
                map.put("date",strings);
            }else if(type.equals("week")){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date newTime = new Date();
                List<Date> days = dateToWeek(newTime);
                for(Date date:days){
                    sum=0.0;
                    String newTime2=sdf.format(date);
                    List<ChargingFlow> chargingFlows = chargingFlowService.list();
                    for (ChargingFlow chargingFlow : chargingFlows) {
                        if (chargingFlow.getUserId() == userId) {
                            String oldTime = chargingFlow.getCreateTime();
                            String[] strs = oldTime.split(" ");
                            String oldTime2 = strs[0];
                            if (newTime2.equals(oldTime2)) {
                                sum = sum + chargingFlow.getMoney();
                            }
                        }
                    }
                    doubles.add(sum);
                }
                strings.add("星期一");
                strings.add("星期二");
                strings.add("星期三");
                strings.add("星期四");
                strings.add("星期五");
                strings.add("星期六");
                strings.add("星期日");
                map.put("date",strings);
                map.put("money",doubles);
            }else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                List<String> dateList = new ArrayList<String>();
                String newTime=sdf.format(new Date());
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
                String oldTime=sdf1.format(new Date());
                String oldTime2=oldTime+"-01";
                Date dateOne = sdf.parse(oldTime2);
                Date dateTwo = sdf.parse(newTime);

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(dateOne);

                dateList.add(sdf.format(dateOne));
                while (calendar.getTime().before(dateTwo)) {
                    calendar.add(Calendar.DAY_OF_MONTH, +1);
                    dateList.add(sdf.format(calendar.getTime()));
                }
                for(String everyDay:dateList){
                    System.out.println("!!!!!!!!!!!!!!!!!"+everyDay);
                    sum=0.0;
                    List<ChargingFlow> chargingFlows = chargingFlowService.list();
                    for (ChargingFlow chargingFlow : chargingFlows) {
                        if (chargingFlow.getUserId() == userId) {
                            String oldTime3 = chargingFlow.getCreateTime();
                            String[] strs = oldTime3.split(" ");
                            String oldTime4 = strs[0];
                            if (everyDay.equals(oldTime4)) {
                                sum = sum + chargingFlow.getMoney();
                            }
                        }
                    }
                    doubles.add(sum);
                    strings.add(everyDay);
                }
                map.put("date",strings);
                map.put("money",doubles);
            }
            return map;
    }

    /**
     * 根据日期获得所在周的日期
     * @param mdate
     * @return
     */
    @SuppressWarnings("deprecation")
    public static List<Date> dateToWeek(Date mdate) {
        int b = mdate.getDay();
        Date fdate;
        List<Date> list = new ArrayList<Date>();
        Long fTime = mdate.getTime() - b * 24 * 3600000;
        for (int a = 1; a <= 7; a++) {
            fdate = new Date();
            fdate.setTime(fTime + (a * 24 * 3600000));
            list.add(a-1, fdate);
        }
        return list;
    }
    @GetMapping("/getVisitors")
    @ResponseBody
    public Integer getVisitors() throws SecurityException, IllegalArgumentException {
        System.out.println("进入获取访问量的方法");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String newTime = sdf.format(new Date());
        List<Log> logs = logService.list();
        System.out.println("获得访问数据集合" + logs);
        int sum = 0;
        for (Log log : logs) {
            Date oldTime = log.getCreateTime();
            String oldTime2 = sdf.format(oldTime);
            if (newTime.equals(oldTime2)) {
                sum = sum + 1;
            }
        }
        System.out.println("输出sum" + sum);
        return sum;
    }

    @GetMapping("/getMember")
    @ResponseBody
    public Integer getMember() throws SecurityException, IllegalArgumentException {
        System.out.println("进入获取访新增会员的方法");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String newTime = sdf.format(new Date());
        List<Registorder> registorders = registorderService.list();
        int sum = 0;
        for (Registorder registorder : registorders) {
            if (registorder.getUserId() == userId) {
                Date oldTime = registorder.getCreateTime();
                String oldTime2 = sdf.format(oldTime);
                if (newTime.equals(oldTime2)) {
                    if (registorder.getOderFlag() == 1) {
                        sum = sum + 1;
                    }
                }
            }
        }
        System.out.println("输出sum" + sum);
        return sum;
    }

    @GetMapping("/getRoom")
    @ResponseBody
    public Integer getRoom() throws SecurityException, IllegalArgumentException {
        QueryWrapper<Room> roomQueryWrapper=new QueryWrapper<Room>();
        roomQueryWrapper.eq("state",1);
        int sum=roomService.count(roomQueryWrapper);
        return sum;
    }
}